package com.etms.service.impl;

import com.etms.mapper.HuopinMapper;
import com.etms.pojo.Goods_category;
import com.etms.pojo.SysUser;
import com.etms.service.HuopinService;
import com.etms.utils.DateUtils;
import com.etms.vo.DangerVo;
import com.etms.vo.PageParmas;
import com.etms.vo.PageResult;
import com.etms.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HuopinServiceImpl implements HuopinService {
    @Autowired
    HuopinMapper huopinMapper;

    @Override
    public Result findList(PageParmas pageParmas) {
        PageHelper.startPage(pageParmas.getCurrentPage(), pageParmas.getPageSize());
        List<Goods_category> list=huopinMapper.findAll();
        PageInfo pageInfo=new PageInfo(list);
        PageResult pageResult=new PageResult();
        pageResult.setList(list);
        pageResult.setTotalPage(pageInfo.getPages());
        return Result.successed(pageResult);
    }

    @Override
    public Result findById(Integer id) {
        Goods_category huopin=huopinMapper.findById(id);
        if(huopin==null){
            return Result.failed();
        }
        return Result.successed(huopin);
    }

    @Override
    public Result delById(Integer id) {
        boolean f=huopinMapper.delById(id);
        if(!f){
            return Result.failed();
        }
        return Result.successed(f);
    }

    @Override
    public Result updateOrAdd(Goods_category goods_category) {
        if(goods_category.getId()!=null&&goods_category.getId()!=0){//走更新
            huopinMapper.update(goods_category);
            return Result.successed("修改成功!",null);
        }
        goods_category.setSumPrice(goods_category.getPerPrice()* goods_category.getSum());
        huopinMapper.addOne(goods_category);
        return Result.successed("新增成功!",null);
    }

    @Override
    public Result baoyang(Integer id) {
        Date date = new Date();
        Goods_category byId = huopinMapper.findById(id);
        String lastProtectTimeStr = DateUtils.getDateTime(date);
        long lastProtectTime = DateUtils.getTimeMills(date);
        byId.setLastProtectTime(lastProtectTime).setLastProtectTimeStr(lastProtectTimeStr);

        huopinMapper.update(byId);
        return Result.successed("保养成功!",null);
    }

    @Override
    public Result getDanger() {
        List<Goods_category> dangerSum =huopinMapper.findDangerSum();
        List<Goods_category> dangerTime =huopinMapper.findDangerTime(DateUtils.getTimeMills(new Date()));
        DangerVo dangerVo=new DangerVo();
        dangerVo.setDangerSum(dangerSum).setDangerTime(dangerTime);

        return Result.successed(dangerVo);
    }

    @Override
    public List<Goods_category> findAll() {
        return huopinMapper.findAll();
    }

    @Override
    public Integer updateSum(long presum, long nowsum, Integer gid) {
        return huopinMapper.updateSum(presum,nowsum,gid);
    }
}
